package com.lzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.base.BaseInfoProperties;
import com.lzy.enums.FriendRequestVerifyStatus;
import com.lzy.enums.YesOrNo;
import com.lzy.mapper.FriendRequestMapper;
import com.lzy.mapper.custom.FriendRequestMapperCustom;
import com.lzy.mapper.FriendshipMapper;
import com.lzy.pojo.FriendRequest;
import com.lzy.pojo.Friendship;
import com.lzy.pojo.bo.NewFriendRequestBO;
import com.lzy.pojo.vo.NewFriendsVO;
import com.lzy.service.FriendRequestService;
import com.lzy.utils.PagedGridResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author lzy
 * @since 2025-08-26
 */
@Service
public class FriendRequestServiceImpl extends BaseInfoProperties implements FriendRequestService {


    @Autowired
    private FriendRequestMapper friendRequestMapper;

    @Autowired
    private FriendRequestMapperCustom friendRequestMapperCustom;

    @Autowired
    private FriendshipMapper friendshipMapper;
    @Transactional
    @Override
    public void addFriendReQuest(NewFriendRequestBO newFriendRequestBO) {
        // 先删除以前的记录
        QueryWrapper<FriendRequest> deleteWrapper = new QueryWrapper<FriendRequest>()
                .eq("my_id",newFriendRequestBO.getMyId())
                .eq("friend_id",newFriendRequestBO.getFriendId());
        friendRequestMapper.delete(deleteWrapper);

        // 新增请求添加记录
        FriendRequest friendRequest = new FriendRequest();
        BeanUtils.copyProperties(newFriendRequestBO,friendRequest);
        friendRequest.setVerifyStatus(FriendRequestVerifyStatus.WAIT.type);
        friendRequest.setRequestTime(LocalDateTime.now());

        friendRequestMapper.insert(friendRequest);
    }

    @Override
    public PagedGridResult queryNewFriendList(String userId, Integer page, Integer pageSize) {

        Map<String,Object> map = new HashMap<>();
        map.put("mySelfId",userId);
        Page<NewFriendsVO> pageInfo = new Page<>(page,pageSize);
        friendRequestMapperCustom.queryNewFriendList(pageInfo,map);

        return setterPagedGridPlus(pageInfo);
    }

    @Override
    @Transactional
    public void passNewFriend(String friendRequestId, String friendRemark) {
        FriendRequest friendRequest = getSingle(friendRequestId);
        String mySelfId = friendRequest.getFriendId(); // 通过方的用户id
        String friendId = friendRequest.getMyId(); // 申请方的用户id

        //创建双方的好友关系
        LocalDateTime nowTime = LocalDateTime.now();
        Friendship friendshipSelf = new Friendship();
        friendshipSelf.setMyId(mySelfId);
        friendshipSelf.setFriendId(friendId);
        friendshipSelf.setFriendRemark(friendRemark);
        friendshipSelf.setIsBlack(YesOrNo.NO.type);
        friendshipSelf.setIsMsgIgnore(YesOrNo.NO.type);
        friendshipSelf.setCreatedTime(nowTime);
        friendshipSelf.setUpdatedTime(nowTime);

        Friendship friendshipOppoSite = new Friendship();;
        friendshipOppoSite.setMyId(friendId);
        friendshipOppoSite.setFriendId(mySelfId);
        friendshipOppoSite.setFriendRemark(friendRequest.getFriendRemark());
        friendshipOppoSite.setIsBlack(YesOrNo.NO.type);
        friendshipOppoSite.setIsMsgIgnore(YesOrNo.NO.type);
        friendshipOppoSite.setCreatedTime(nowTime);
        friendshipOppoSite.setUpdatedTime(nowTime);

        friendshipMapper.insert(friendshipSelf);
        friendshipMapper.insert(friendshipOppoSite);

        // A通过B的请求后，需要把双方的好友请求设置置为通过
        friendRequest.setVerifyStatus(FriendRequestVerifyStatus.SUCCESS.type);
        friendRequestMapper.updateById(friendRequest);

        // 如果A添加B，B没有通过反而B添加A，如果A通过请求则双方的请求都需要通过
        QueryWrapper<FriendRequest> queryWrapper = new QueryWrapper<FriendRequest>()
                .eq("my_id",mySelfId)
                .eq("friend_id",friendId);
        FriendRequest friendRequestOppoSite = friendRequestMapper.selectOne(queryWrapper);
        if (friendRequestOppoSite != null){
            friendRequestOppoSite.setVerifyStatus(FriendRequestVerifyStatus.SUCCESS.type);
        }
    }


    private FriendRequest getSingle(String friendRequestId){
        return friendRequestMapper.selectById(friendRequestId);
    }
}
